package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.mapper.MagnetMapper;
import com.woniu.pojo.Magnet;
import com.woniu.service.IMagnetService;
import com.woniu.vo.MagnetQueryVo;
import com.woniu.vo.MagnetReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author long
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/magnet")
@Api(tags = "地磁管理")
public class MagnetController extends BaseController{
    @Autowired
    private IMagnetService magnetService;


    @ApiOperation("分页搜索")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result search(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody MagnetQueryVo magnetQueryVo){
        PageResult pageResult = magnetService.search(currentPage, pageSize, magnetQueryVo);
        return toDataResult(pageResult);
    }

    @ApiOperation("添加地磁")
    @PostMapping
    public Result add(@RequestBody Magnet magnet){
        boolean flag = magnetService.save(magnet);
        return toResult(flag);
    }

    @ApiOperation("删除地磁")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        boolean flag = magnetService.removeById(id);
        return toResult(flag);
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Long id){
        Magnet magnet = magnetService.getById(id);
        return toDataResult(magnet);
    }

    @ApiOperation("修改地磁")
    @PutMapping
    public Result edit(@RequestBody Magnet magnet){
        boolean flag = magnetService.updateById(magnet);
        return toResult(flag);
    }
    @ApiOperation("查询所有")
    @GetMapping
    public Result queryAll(){
        List<Magnet> magnetList = magnetService.list();
        return toDataResult(magnetList);
    }


}
